package com.alibaba.otter.manager.web.resource;

import com.alibaba.otter.manager.biz.dto.CommonSearchDTO;
import com.alibaba.otter.manager.biz.config.datamediasource.DataMediaSourceService;
import com.alibaba.otter.manager.biz.utils.DataSourceChecker;
import com.alibaba.otter.manager.web.dto.RestResponse;
import com.alibaba.otter.manager.web.service.DataMediaSourceAggService;
import com.alibaba.otter.manager.web.utils.PaginationUtil;
import com.alibaba.otter.shared.common.model.config.data.DataMediaSource;
import com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据源配置
 */
@RequestMapping("/api/v1/data-media-source")
@RestController
public class DataMediaSourceResource {

    private final DataMediaSourceService dataMediaSourceService;

    private final DataSourceChecker dataSourceChecker;

    private final DataMediaSourceAggService dataMediaSourceAggService;

    public DataMediaSourceResource(DataMediaSourceService dataMediaSourceService, DataSourceChecker dataSourceChecker, DataMediaSourceAggService dataMediaSourceAggService) {
        this.dataMediaSourceService = dataMediaSourceService;
        this.dataSourceChecker = dataSourceChecker;
        this.dataMediaSourceAggService = dataMediaSourceAggService;
    }


    @GetMapping("findOne/{id}")
    public RestResponse<DataMediaSource> findOne(@PathVariable("id") Long id){
        return RestResponse.ok(dataMediaSourceService.findById(id));
    }


    @GetMapping("list")
    public RestResponse<List<DataMediaSource>> list(CommonSearchDTO searchVo, Pageable pageable){
        return PaginationUtil.pageHttpResponse(dataMediaSourceService.dataMediaSourcePage(searchVo, pageable));
    }


    @PostMapping("add")
    public RestResponse<Void> add(@RequestBody @Validated DbMediaSource dbMediaSource){
        dataMediaSourceAggService.create(dbMediaSource);
        return RestResponse.ok();
    }


    @PutMapping("edit")
    public RestResponse<Void> edit(@RequestBody @Validated DbMediaSource dbMediaSource){
        dataMediaSourceAggService.modify(dbMediaSource);
        return RestResponse.ok();
    }


    @DeleteMapping("delete/{id}")
    public RestResponse<Void> delete(@PathVariable("id") Long id){
        dataMediaSourceService.remove(id);
        return RestResponse.ok();
    }


    @PostMapping("data-source-check")
    public RestResponse<String> dataSourceCheck(@RequestParam("dataSourceId") Long dataSourceId){
        return RestResponse.ok(dataSourceChecker.dataSourceCheck(dataSourceId));
    }
    /**
     * 检查数据库链接的有效性
     */
    @PostMapping("check")
    public RestResponse<String> check(@RequestBody @Validated DbMediaSource db){
        String result = dataSourceChecker.check(db.getUrl(),
                db.getUsername(),
                db.getPassword(),
                db.getEncode(),
                db.getType().name());
        return RestResponse.ok(result);
    }
}
